package org.suanfaTranning.ST200_Chatpter1_LineTable_HashTable_String.linearList;

import org.jikeshijian.Solution;

/**
 * @Auther: qingle
 * @Date: 2024/9/10-18:27
 * @Description:
 * @version: 1.0
 */
public class Sloution_twoSum {
	public static int add(int a, int b) {
		while (b != 0) {
			// 计算不带进位的和
			int sum = a ^ b;
			// 计算进位
			int carry = (a & b) << 1;
			// 更新a为新的和，b为新的进位
			a = sum;
			b = carry;
		}
		return a;
	}

//	public static int addWithOverflow(int a, int b) {
//		int sign = (a < 0) ^ (b < 0) ? -1 : 1;
//		a = Math.abs(a);
//		b = Math.abs(b);
//
//		while (b != 0) {
//			int sum = a ^ b;
//			int carry = (a & b) << 1;
//			a = sum;
//			b = carry;
//		}
//
//		return sign * a;
//	}

	public static void main(String[] args) {
		Solution solution = new Solution();
		System.out.println(Sloution_twoSum.add(1, 2)); // 输出: 3
		System.out.println(Sloution_twoSum.add(-2, 3)); // 输出: 1
//		System.out.println(Sloution_twoSum.addWithOverflow(1900000000, 1900000000)); // 输出:
	}
}
